package com.heiyanqiang.dao;

import com.heiyanqiang.domain.Permission;
import com.heiyanqiang.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RoleDao {
    /*
    *     private Integer id;
    private String roleName;
    private String roleDesc;
    private List<Permission> permissions;
    private List<UserInfo> users;
    * */

    //根据id查询出角色所有的信息
    @Select("select * from role where id in (select roleId from users_role where userId=#{userId})")
    @Results({
            @Result(id = true, property = "id",column = "id"),
            @Result(property = "roleName",column = "roleName"),
            @Result(property = "roleDesc",column = "roleDesc"),
            @Result(property = "permissions",column = "id",javaType = java.util.List.class,many = @Many(select = "com.heiyanqiang.dao.PermissionsDao.findPermissionByRoleId"))

    })
    Role findRoleByUserId(String userId) throws Exception;
    //查询所有规则
    @Select("select * from role")
    List<Role> findAll() throws Exception;

    //添加角色
    @Insert("insert into role (roleName,roleDesc) values (#{roleName},#{roleName})")
    void save(Role role);

    //根据roleId查询role
    @Select("select * from role where id = #{roleId}")
    Role findById(Integer roleId);

    //根据roleId查询permissions
    @Select("select * from permission where id not in (select permissionId from role_permission where roleId= #{roleId})")
    List<Permission> findOtherPermissions(Integer roleId);

    //添加权限到角色
    @Insert("insert into role_permission (roleId,permissionId) values (#{roleId},#{permissionId})")
    void addPermissionToRole(@Param("roleId") Integer roleId, @Param("permissionId") Integer permissionId);
}
